<?php
//included headers and classes
include "header.php";
$P = new cMyPage;
$P->loadHeader();
$P->loadTitle("Welcome");
$locList = array();
$loc['url'] = '/index.php';
$loc['name'] = 'Home';
array_push($locList, $loc);
$loc['url'] = '/userinfo.php';
$loc['name'] = 'User Information';
array_push($locList, $loc);
$P->loadLocation($locList);
$P->loadLinks();
$P->loadPreContent("Profile");

//if user is logged in
if($P->isLoggedIn()) 
{
//getting HTTP GET parameters (used to dispay other peoples' pages)
	$user_td=NULL;
	$own_page=0;
	$uid=$_GET["uid"];
//TODO - make a check for RPI users/friends
	switch ($uid)
	{
		case ($uid==NULL):
//user to display - display own page
			$user_td=$user->data['user_id'];
			$own_page=1;
			break;
		case ($uid==$user->data['user_id']):
			$user_td=$user->data['user_id'];
			$own_page=1;
			break;
		default:
			$user_td=$uid;
			$own_page=0;
		break;
	}
	if (($own_page==1)||($P->isFriend($uid,$user->data['user_id']))||($P->isRPI()))
	{

	//output of user data
	//select data from DB
		$sql_userinfo="SELECT a.name, username, b.user_website, b.user_interests, b.user_from, a.YearGrad, a.Department, a.Concentration, a.Degree_Undergrad, a.Degree_Grad, a.Degree_Phd, a.e_name, a.e_location, a.e_datestarted, a.e_title, a.e_department, a.e_website, b.user_email, user_interests FROM `users` a join `phpbb_users` b on a.id = b.user_id  where a.id = '".$user_td."'";
		$squery=$db->sql_query($sql_userinfo);
		$user_data=$db->sql_fetchrow($squery);
		echo '<div style="float:right;">'; 

		//var_dump($user_data);
		if ($own_page==1)
		{
			//select friends who has added this user
			// TODO add a check for unconfirmed
			$sql_select_friends="SELECT username, user_email, name, id FROM friends f join users u on f.User1_id = u.id join phpbb_users pu on User1_id = pu.user_id WHERE user2_id ='".$user->data['user_id']."' AND `status` = 'unconfirmed'";
			$fquery=$db->sql_query($sql_select_friends);
			if($db->sql_affectedrows() > 0) {
				echo "Pending friend requests<br />";
			while ($friend_data=$db->sql_fetchrow($fquery))
			{
//		$friend_data=$db->sql_fetchrow($fquery);
// TODO - place button with Confirm Friend functionality
				echo '<span class="linkB" id="confirmFriend_'.$friend_data['id'].'">Add '.$friend_data['name'].'</a><br />';
			}
			}
		}
		}
	?>
   <?
	//Friend Status
	$fstatus = $P->friendStatus($_GET['uid']);
	if( !isset($_GET['uid']) )
		$profileAccess = true;
	elseif( $_GET['uid'] == $data->user['user_id'] )
		$profileAccess = true;
	elseif( $P->isRPI() ) {
		$profileAccess = true;
	} elseif ( $fstatus == "confirmed" ) {
		$profileAccess = true;
	} else {
		$profileAccess = false;
	} 

	
	if(isset($_GET['uid']) && $own_page == 0) {
		if($fstatus == "NO") {
			echo '<span id="addFriendBtn"><input type="button" id="requestFriendship_'.$uid.'" class="btn" value="Request Friendship" /></span><br/>';
		} elseif( $fstatus == "unconfirmed" ) {
			echo 'Friendship Pending<br />';
		}
		
		
	}
	
		
	//Resume
	$sql = "SELECT * FROM resumes WHERE id = '".$user->data['user_id']."'";
	$query = $db->sql_query($sql);
	if($db->sql_affectedrows() > 0 && $profileAccess == true)
	{
		echo "<h3>Resume Download</h3>";
	  while($row = $db->sql_fetchrow($query))
	  {
	  ?>
			 
         <a href="viewres.php?id=<? echo $row['id']; ?>"><? echo $row['name']; ?></a> <br />
  	<?
  		}
	}
	
	?>
   </div>
  <? if($profileAccess == true) { ?>
  <table border='0'>
  <tr>
    <td>Full Name</td>
    <td> <?php  echo $user_data['name']; ?> </td>
  </tr>
  
  <tr>
  	<td>Forum Name</td>
   <td><?=$user_data['username']; ?></td>
   </tr>
  
  <tr>
    <td>Location</td>
    <td> <?php echo $user_data['user_from'];  ?> </td>
  </tr>
  
  <tr>
  <td>Graduation Year</td>
  <td> <?=$user_data['YearGrad']; ?></td>
  </tr>
  
  <tr>
    <td>Department</td>
    <td> <?=$user_data['Department']; ?> </td>
  </tr>
  
  <tr>
    <td>Concentration</td>
    <td> <?=$user_data['Concentration']; ?> </td>
  </tr>
  
  <tr>
    <td>Degrees Attained</td>
    <td> <? 
	 $deg = array();
	 if($user_data['Degree_Undergrad'] == 1) {
	 	array_push($deg, "Undergraduate");
	 
	 } 
	 
	 if($user_data['Degree_Grad'] == 1)	
	 	array_push($deg, "Graduate");
		
	if($user_data['Degree_Phd'] == 1)
		array_push($deg, "Doctorate");
	 
	 foreach($deg as $v) {
	 	echo $v;
		if($v != end($deg))
			echo ", ";
	}
	 
	 
	 ?> 
    
    
    </td>
  </tr>
  
  <tr>
  <td>Interests</td>
  <td><?=$user_data['user_interests']; ?>
  
  <tr>
    <td>Website</td>
    <td> <?php echo $user_data['user_website'];  ?> </td>
  </tr>
  
  
  <tr>
    <td>Email</td>
    <td> <?php 
	//generate email image	
	$wMult = 1;
if( strlen($user_data['user_email']) > 0 ) 
	$wMult = strlen( $user_data['user_email'] );
$w = (int)7*$wMult;
$im = imagecreate($w, 30);
// White background and blue text
$bg = imagecolorallocate($im, 255, 255, 255);
$textcolor = imagecolorallocate($im, 0, 0, 255);
// Write the string at the top left
imagestring($im, 3, 0, 0, $user_data['user_email'], $textcolor);
// start buffering
ob_start();
// output jpeg (or any other chosen) format & quality
imagepng($im);
// capture output to string
$contents = ob_get_contents();
// end capture
ob_end_clean();
	
echo '<img src="data:image/png;base64,'. base64_encode($contents). '"/>';

	
	  ?> 
     </td>
  </tr>
  
  </table>

  
 <table border='0'>
	<tr>
   <td align="center" colspan="2"><b>Employer Information</b></td>
   </tr>

  <tr>
    <td>Current employer</td>
    <td><?php echo $user_data['e_name']; ?></td>
  </tr>
    <tr>
    <td>Title</td>
    <td><?php echo $user_data['e_title']; ?></td>
  </tr>
  <tr>
  <td>Department</td>
  <td><?=$user_data['e_department']; ?>
  </tr>
    <tr>
    <td>Date Started</td>
    <td><?php echo $user_data['e_datestarted']; ?></td>            
  </tr>
  <tr>
  <td>Company Location</td>
  <td><?=$user_data['e_location']; ?></td>
  </tr>
  <tr>
  <td>Company Website</td>
  <td><?=$user_data['e_website']; ?></td>
  </tr>
</table>
    <?php 
	} else {
		echo "<p>A mutual friendship must be established with this user before viewing their profile.</p>";
	} 	
		// check if this is not user himself and he is not a friend
	} 
else {
//not logged in
echo 'Please Log In';
	$P->displayLogin();
} 
?>

<?
$P->loadFooter();
?>